#!/usr/bin/env python
# -*- coding: UTF-8 -*-

'''GUI for http://en.wikipedia.org/wiki/Langton's_ant'''

import sys

from cellular_automata.cellular_engine.constants import WORLD_TYPE_TORUS
from cellular_automata.langtons_ant import BasicLangtonsAnt

import pygame
from pygame.locals import *

pygame.init()

GAME_SIZE = (512, 512)
TILE_SIZE = (4, 4)

COLOR_ALIVE = (0, 0, 0)
COLOR_DEAD = (255, 255, 255)
COLOR_BORDER = (100, 100, 100)
COLOR_ANT = (255, 0, 0)

DELAY = 0


def draw_to_surface(langtons_ant, surface):
    '''Draw a LangtonsAnt object to a PyGame surface'''
    position, state = langtons_ant.old_position

    if state:
        surface.blit(BLACK_CELL_IMAGE, position)
    else:
        surface.blit(WHITE_CELL_IMAGE, position)

    surface.blit(ANT_CELL_IMAGE, langtons_ant.position)

    pygame.display.update(((position, TILE_SIZE),
                           (langtons_ant.position, TILE_SIZE)))

if __name__ == '__main__':
    surface = pygame.display.set_mode(GAME_SIZE)
    surface.convert()
    surface.fill(COLOR_DEAD)
    pygame.display.update()
    pygame.display.set_caption('Langtons Ant')

    BLACK_CELL_IMAGE = pygame.Surface(TILE_SIZE)
    BLACK_CELL_IMAGE = BLACK_CELL_IMAGE.convert()
    BLACK_CELL_IMAGE.fill(COLOR_ALIVE)
    pygame.draw.rect(BLACK_CELL_IMAGE, COLOR_BORDER, BLACK_CELL_IMAGE.get_rect(), 1)

    WHITE_CELL_IMAGE = pygame.Surface(TILE_SIZE)
    WHITE_CELL_IMAGE = WHITE_CELL_IMAGE.convert()
    WHITE_CELL_IMAGE.fill(COLOR_DEAD)

    ANT_CELL_IMAGE = pygame.Surface(TILE_SIZE)
    ANT_CELL_IMAGE = ANT_CELL_IMAGE.convert()
    ANT_CELL_IMAGE.fill(COLOR_ANT)

    bPause = True
    the_game = BasicLangtonsAnt(GAME_SIZE, TILE_SIZE, WORLD_TYPE_TORUS)
    # ^- you just lost it

    def quit_everything():
        pygame.quit()
        sys.exit()

    while True:
        for event in pygame.event.get():
            e_type = event.type

            if e_type == QUIT:
                quit_everything()
            elif e_type == MOUSEBUTTONDOWN and event.button == 1:
                the_game.toggle_state_at_position(event.pos)
                draw_to_surface(the_game, surface)
            elif e_type == KEYDOWN:
                key = event.key

                if key == K_SPACE:
                    bPause = not bPause
                elif key == K_3 and bPause:
                    the_game.game_loop()
                    draw_to_surface(the_game, surface)
                elif key == K_g:
                    print('Generation: %i' % the_game.generation)
                elif key == K_UP:
                    DELAY += 1
                    print('Changed delay to: {0}'.format(DELAY))
                elif key == K_DOWN:
                    if DELAY > 0:
                        DELAY -= 1
                        print('Changed delay to: {0}'.format(DELAY))
                elif key == K_ESCAPE:
                    quit_everything()

        if not bPause:
            the_game.game_loop()
            draw_to_surface(the_game, surface)

            pygame.time.delay(DELAY)
